package com.example.springbootgoods.controller;

import com.example.springbootgoods.entity.Commodity;
import com.example.springbootgoods.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/")
public class CommodityController {
    @Autowired
    CommodityService commodityService;
    @GetMapping
    public String index( @RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                         @RequestParam(required = false,defaultValue = "2")   Integer pageSize,
                         Model model){
        model.addAttribute("commodityPage",commodityService.selectPage(pageIndex,pageSize));
        return "index";
    }

    @GetMapping("/details/{cid}")
    public String details(Model model,@PathVariable int cid){
        model.addAttribute("detail",commodityService.selectById(cid));
        return "details";
    }

    @GetMapping("/toInsert")
    public String toInsert(){
        return "insert";
    }

    @PostMapping("/doInsert")
    public String doInsert(Commodity commodity){
       int i= commodityService.insert(commodity);
        System.out.println(i);
        return  "redirect:";
    }
}
